<?php
namespace Activity\Model;
use Think\Model;
class ActivityModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_activity'; 
	
	public function getActivityList($data){
		if(!empty($data['custid'])){
			$map['custid']  = $data['custid'];
		}
		
		if(!empty($data['custids']) && is_array($data['custids'])){
			$map['custid']  = array('in' , $data['custids']);
		}
		
		if($data['status'] === "0" || $data['status'] === "1"){
			$map['status']  = $data['status'];
		}
		
		$total = $this->where($map)->count();
		$fields = array('activityid','custid','title','description','status','cover','starttime','endtime','personnum','acceptnum','address','fee','createtime','updatetime');
		
		if(empty($data['sortField']))$data['sortField'] = "activityid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
		
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else 
			$result = $this->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
		
		return array('total'=>$total , 'data'=>$result);
	}
	
	public function getActivityById($activityid){
		$map['activityid'] = $activityid;
		$fields = array('activityid','custid','title','description','status','cover','starttime','endtime','personnum','acceptnum','address','fee','createtime','updatetime');
		$result = $this->field($fields)->where($map)->find();
		return $result;
	}
	
	public function getJoinList($data){
		$map['qyx_activity_member.custid'] = $data['custid'];
		$map['qyx_activity.status']  = 1;
		
		$total = $this->join('qyx_activity_member on qyx_activity.activityid=qyx_activity_member.activityid')->where($map)->count();
		
		if(empty($data['sortField']))$data['sortField'] = "qyx_activity.activityid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
		
		$fields = array('qyx_activity.activityid','qyx_activity.custid','qyx_activity.title','qyx_activity.description','qyx_activity.cover',
						'qyx_activity.starttime','qyx_activity.endtime','qyx_activity.personnum','qyx_activity.address','qyx_activity.fee','qyx_activity_member.status',
						'qyx_activity.createtime','qyx_activity.updatetime');
		
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->join('qyx_activity_member on qyx_activity.activityid=qyx_activity_member.activityid')->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else
			$result = $this->join('qyx_activity_member on qyx_activity.activityid=qyx_activity_member.activityid')->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
		
		return array('total'=>$total , 'data'=>$result);
	}
	
	public function addActivity($data){
		$result = $this->add($data);
		return $result;
	}
	
	public function updateActivity($data){
		$map['activityid'] = $data['activityid'];
		$map['custid'] = $data['custid'];
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	public function deleteActivity($data){
		$map['activityid'] = $data['activityid'];
		$map['custid'] = $data['custid'];
		$result = $this->where($map)->delete();
		return $result;
	}
	
	public function acceptInc($activityid){
		$activityid = (int)$activityid;
		$sql = "update qyx_activity set acceptnum=acceptnum+1 where activityid={$activityid}";
		$this->execute($sql);
	}
}


